package cn.edu.qfnu.oj.service.impl;

import cn.edu.qfnu.common.service.Impl.BaseServiceImpl;
import cn.edu.qfnu.oj.dao.ContestDao;
import cn.edu.qfnu.oj.entity.Contest;
import cn.edu.qfnu.oj.entity.Problem;
import cn.edu.qfnu.oj.service.ContestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Set;

@Service("contestService")
public class ContestServiceImpl extends BaseServiceImpl<Contest, Integer> implements ContestService {

    private final ContestDao contestDao;

    @Autowired
    public ContestServiceImpl(ContestDao contestDao) {
        this.contestDao = contestDao;
    }

    @Override
    public Contest modify(Contest contest, String title, String content,
                   String start, String duration, Set<Problem> problems) {

        if (!title.equals(contest.getTitle())) {
            contest.setTitle(title);
        }
        if (!content.equals(contest.getContent())) {
            contest.setContent(content);
        }
        if (!LocalDateTime.parse(start).isEqual(contest.getStartTime())) {
            contest.setStartTime(LocalDateTime.parse(start));
        }
        if (!LocalDateTime.parse(start).plusDays(Long.parseLong(duration)).isEqual(contest.getEndTime())) {
            contest.setEndTime(LocalDateTime.parse(start).plusDays(Long.parseLong(duration)));
        }
        if (!problems.equals(contest.getProblems())) {
            contest.setProblems(problems);
        }

        return contest;
    }
}
